<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }

    function update_news_hit($aid){
        $ci =& get_instance();
        $ci->db->where("aid","$aid");
        $ci->db->set("hit","hit+1",false);
        $ci->db->update('news');
        $ci->db->close();
    }

    function getallnews($num=1,$offset=0)
    {    	   
		if($offset>0){$offset=$offset*$num;}else{$offset=0;}
	    $this->db->select('aid,title,images,tags,postdate,brief,source');
		$this->db->limit($num,$offset);   
	    $this->db->from("news");
	   $this->db->order_by('isNew','desc');
	    $this->db->order_by('Aid','desc');
	    $this->db->order_by('orderby','asc');
	    $query = $this->db->get(); 
	    $list=$query->result_array();
	   foreach($list as $key=>$rs){
	       $rs['url']=site_url('news/id')."/".$rs['aid'];
	       $rs['postdate']=date('Y/m/d H:i',strtotime($rs['postdate']));
	       $list[$key]=$rs;
	    }	      
	    return $list;   
        $query->free_result();
		$this->db->close();
    }
    function gettopnews($num=0)
    {    
    	$this->load->helper('MY_string');	   
	    $this->db->select('aid,title,postdate,tags');  
	    $this->db->from("news");
	    $this->db->order_by('Aid','desc');
	    $this->db->order_by('orderby','asc');
	    $this->db->limit($num);  
	    $query = $this->db->get(); 
		$list=$query->result_array();
	    foreach($list as $key=>$rs){
	       $rs['title']=get_utf8_word($rs['title'],24,1,"...");
	       $rs['url']=site_url('news/id')."/".$rs['aid'];
	       $list[$key]=$rs;
	    }	    
	    return $list; 
        $query->free_result();
		$this->db->close();
    }
    function gethotnews($num=0)
    {    
    	$this->load->helper('MY_string');	   
	    $this->db->select('aid,title,postdate,tags');  
	    $this->db->from("news");
	    $this->db->where("isTop","1");
	    $this->db->order_by('Aid','desc');
	    $this->db->order_by('orderby','asc');
	    $this->db->limit($num);  
	    $query = $this->db->get(); 
		$list=$query->result_array();
	    foreach($list as $key=>$rs){
	       $rs['title']=get_utf8_word($rs['title'],24,1,"...");
	       $rs['url']=site_url('news/id')."/".$rs['aid'];
	       $list[$key]=$rs;
	    }	    
	    return $list; 
        $query->free_result();
		$this->db->close();
    }
    
    function getonenews($aid=0)
    {
	    $this->db->where('aid',"$aid");
	    $this->db->limit(1); 
	    $query = $this->db->get('news');	   
	    if ($query->num_rows()>0) {
			 $list=$query->result_array();
			 $this->update_news_hit($aid);
		    $query->free_result();
		    $this->db->close();
	    	foreach($list as $key=>$rs){
	 			$rs[$key]=$rs;
	 		}
		    return $rs;
	    }else{show_404();}
    }

    
    function getrandnews($num=0)
    { 
	    $this->db->select('aid,title,postdate,tags');  
	     $this->db->order_by('orderby','random');
	      $this->db->limit($num);
	    $query = $this->db->get('news');	   
	    if ($query->num_rows()>0) {
			 $list=$query->result_array();
	     foreach($list as $key=>$rs){
	       $rs['url']=site_url('news/id')."/".$rs['aid'];
	       $list[$key]=$rs;
	    }
		    $query->free_result();
		    $this->db->close();
		    return $list;
	    }

    }

}
